package com.sda.huoshui.view

import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.PagerAdapter
import java.util.*

/**
 * Created by Administrator on 2018/7/13/013.
 */

class TabViewPagerAdapter(
    fm: FragmentManager,
    fragmentList: List<Fragment>,
    titleList: List<String>
) : FragmentStatePagerAdapter(fm) {

    private var fragmentList = ArrayList<Fragment>()
    private var titleList: List<String> = ArrayList()

    init {
        this.fragmentList = fragmentList as ArrayList<Fragment>
        this.titleList = titleList
    }

    override fun getItem(position: Int): Fragment {
        return fragmentList[position]
    }

    override fun getCount(): Int {
        // Show 4 total pages.
        return fragmentList.size
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return titleList[position]
    }

    fun setPageTitle(titles: List<String>) {
        this.titleList = titles
        notifyDataSetChanged()
    }

    override fun getItemPosition(`object`: Any): Int {
        return if (fragmentList.indexOf(`object`) >= 0) {
            super.getItemPosition(`object`)
        } else {
            PagerAdapter.POSITION_NONE
        }
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
    }
}
